// pages/index/getDiscount/getDiscount.js
var app = getApp(),that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    coupons: [],
    page: 1,
    limit: 10,
    isLoadMore: true,
    isJzz: false,
    isJzwb: false,
    bizUserInfo:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    that.setData({
      bizUserInfo: wx.getStorageSync('bizUserInfo')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    that = this;
    that.getCoupons()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
// 更多优惠券
  getCoupons:function(){
    app.getData({
      url: app.api.getCoupons(that.data.page, that.data.limit),
      method: 'POST',
      data: {
        memberId: that.data.bizUserInfo.id,
        companyId: app.globalData.companyId
      },
      ok: function (res) {
        console.log(res)
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            if (that.data.page == 1) {
              that.data.coupons = []
            }
            if (that.data.coupons.length == res.data.totals){
              that.setData({
                isLoadMore: false,
                isJzz: true
              })
            }else if (res.data.list.length < that.data.limit) {
              that.setData({
                coupons: that.data.coupons.concat(res.data.list),
                isLoadMore: false,
                isJzz:false
              })
            } else {
              that.setData({
                coupons: that.data.coupons.concat(res.data.list),
                isLoadMore: true,
                page: that.data.page + 1
              })
            }
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
          }
        }
      }

    })
  },
  // 立即领取
  onRceive: function(e) {
    console.log(e)
    if (app.isLogin()) {
      //写跳转，或者其他
      app.getData({
        url: app.api.getRceive(),
        method: 'GET',
        data: {
          couponId: e.currentTarget.dataset.id,
          memberId: that.data.bizUserInfo.id
        },
        ok: function (res) {
          console.log(res)
          if (res.statusCode == 200 && res.errMsg == 'request:ok') {
            if (res.data.code == 1) {
              that.setData({
                coupons: res.data.list
              }),
                wx.showToast({
                  title: res.data.message,
                  icon: 'none'
                })
              that.onShow()

            } else {
              wx.showToast({
                title: res.data.message,
                icon: 'none'
              })
            }
          }
        }

      })
    }
    
  },
  //小程序触底方法
  onReachBottom: function (res) {
    if (that.data.isLoadMore) {
      that.getCoupons()
    } else {
      that.setData({
        isJzwb: true,
        isJzz: true
      })
      wx.showToast({
        title: '没有更多数据',
        icon: 'none'
      })
    }
  }
})